﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class control_Pager : System.Web.UI.UserControl
{
    public string link = "";
    public int currentpage = 1;
    public int numberpage = 1;
    public string paramPageName = "";
    public string[] moreParamName = new string[0];
    public string[] moreParamValue = new string[0];

    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    public void BindData()
    {
        pager.Controls.Clear();
        // add back page
        if (currentpage > 1)
        {
            HyperLink backPage = new HyperLink();
            backPage.Text = "Trước";
            string param = "?" + paramPageName + "=" + (currentpage - 1).ToString();
            for (int i = 0; i < moreParamName.Length; i++)
            {
                param += "&" + moreParamName[i] + "=" + moreParamValue[i];
            }
            backPage.NavigateUrl = link + param;
            pager.Controls.Add(backPage);
        }
        // add pages before current page
        for (int i = currentpage - 5; i < currentpage; i++)
        {
            if (i <= 0)
                continue;
            HyperLink temp = new HyperLink();
            temp.Text = i.ToString();
            string param = "?" + paramPageName + "=" + i.ToString();
            for (int j = 0; j < moreParamName.Length; j++)
            {
                param += "&" + moreParamName[j] + "=" + moreParamValue[j];
            }
            temp.NavigateUrl = link + param;
            pager.Controls.Add(temp);
        }

        // add current page
        HyperLink current = new HyperLink();
        current.Attributes["class"] = "active";
        current.Text = currentpage.ToString();
        current.NavigateUrl = "";
        pager.Controls.Add(current);

        // add pages after current page

        for (int i = currentpage + 1; i <= currentpage+5; i++)
        {
            if (i > numberpage)
                break;
            HyperLink temp = new HyperLink();
            temp.Text = i.ToString();
            string param = "?" + paramPageName + "=" + i.ToString();
            for (int j = 0; j < moreParamName.Length; j++)
            {
                param += "&" + moreParamName[j] + "=" + moreParamValue[j];
            }
            temp.NavigateUrl = link + param;
            pager.Controls.Add(temp);
        }

        // add next page
        if (currentpage < numberpage)
        {
            HyperLink nextPage = new HyperLink();
            nextPage.Text = "Tiếp";
            string param = "?" + paramPageName + "=" + (currentpage + 1).ToString();
            for (int i = 0; i < moreParamName.Length; i++)
            {
                param += "&" + moreParamName[i] + "=" + moreParamValue[i];
            }
            nextPage.NavigateUrl = link + param;
            pager.Controls.Add(nextPage);
        }
    }
}
